package com.project.card;

import static com.project.card.util.UrlUtil.SERVER_URL;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;
import com.project.card.ui.BusinessFragment;
import com.project.card.ui.HomeFragment;
import com.project.card.ui.MarketFragment;
import com.project.card.ui.ProfileFragment;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView mNavView;

    private FrameLayout mNavContainer;

    private HomeFragment mHomeFragment;
    private BusinessFragment mBusinessFragment;
    private MarketFragment mMarketFragment;
    private ProfileFragment mProfileFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        

        new Thread() {
            @Override
            public void run() {
                super.run();
                Log.d("pbd", "start");
                Log.d("pbd", ping().toString());
            }
        }.start();

        mHomeFragment=new HomeFragment();
        mBusinessFragment=new BusinessFragment();
        mMarketFragment=new MarketFragment();
        mProfileFragment=new ProfileFragment();

        mNavContainer=findViewById(R.id.nav_container);

        mNavView=findViewById(R.id.nav_view);
        mNavView.setOnItemSelectedListener(mBtnNavListener);

        switchFragment(mHomeFragment);

    }

    private NavigationBarView.OnItemSelectedListener mBtnNavListener
            =new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id=item.getItemId();
            if(id==R.id.navigation_home){
                switchFragment(mHomeFragment);
            }else if(id==R.id.navigation_business){
                switchFragment(mBusinessFragment);
            }else if(id==R.id.navigation_market){
                switchFragment(mMarketFragment);
            }else if(id==R.id.navigation_profile){
                switchFragment(mProfileFragment);
            }
            return true;
        }
    };

    private void switchFragment(Fragment fragment){
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.nav_container,fragment).commitNow();
    }

    private NavigationView.OnNavigationItemSelectedListener mNavItemListener=
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id=item.getItemId();
                    if(id==R.id.navigation_home){

                    }else if(id==R.id.navigation_business){

                    }else if(id==R.id.navigation_market){

                    }else if(id==R.id.navigation_profile){

                    }
                    return false;
                }
            };

    public static Boolean ping(){
        try {
            URL url = new URL(SERVER_URL);
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("User-Agent", "Android Application");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500);
            urlc.connect();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            Log.e("Network Check", "Error checking internet connection", e);
        }
        return false;
    }
}